<!--
// 
// Index.vue
// 客户后台
// 
// 2024/10/23.
// 
-->
<template>
    <div class="Index">
        <div class="tabs">
            <div v-for="item in state.programList" :key="item.id" :class="{active: item.id === state.currentProgram.id}" class="program" @click="programSelect(item)">
                {{ item.name }}
                <transition v-show="item.id === state.currentProgram.id" name="tab-show">
                    <div class="bottom-line"></div>
                </transition>
            </div>
        </div>
        <div class="container">
            <div class="current-version">
                <div class="info">
                    <div class="left">
                        <div class="program-name"> <img class="logo" src="@/assets/logos/logo_02.png"/><span class="name">{{state.currentProgram.name}}</span></div>
                        <div class="program-desc">{{state.currentProgram.description}}</div>
                    </div>
                    <div class="right">
                        <div class="version-num"><el-tag type="danger" effect="dark">New</el-tag> V{{state.currentVersion.versionNum}}</div>
                        <div class="time">发布时间：{{ $format.dateTime(state.currentVersion.createdTime) }}</div>
                        <div @click="downloadAllPackage(state.currentVersion)" class="download-button">
                            <span>立即下载</span>
                        </div>
                    </div>
                </div>
                <div class="log-list">
                    <div class="title">版本日志</div>
                    <div v-if="state.currentVersion.description" class="log-content" v-html="description(state.currentVersion.description)"></div>
                    <div v-else class="log-content placeholder">暂无版本信息</div>
                </div>
            </div>
            <div v-loading="state.isLoading" class="history-version">
                <div v-if="!state.showHistoryVersion" class="list">
                    <div class="title"><el-divider>历史版本</el-divider></div>
                    <el-timeline style="width: calc(100% - 20px);padding-left: 10px;">
                        <el-timeline-item v-for="(item, index) in filterVersionList" :key="index" type="primary">
                            <VersionCell :item="item" @download="downloadAllPackage"/>
                        </el-timeline-item>
                    </el-timeline>
                </div>

                <el-button v-if="state.showHistoryVersion" link type="primary" @click="reLoadVersionList">
                    <el-icon style="margin-right: 4px">
                        <ArrowDownBold />
                    </el-icon>
                    查看更多历史版本
                </el-button>
                <template v-else>
                    <el-button v-if="requestParams.pageIndex < pageConfig.totalPages" link type="primary" @click="nextPage">
                        <el-icon style="margin-right: 4px">
                            <ArrowDownBold />
                        </el-icon>
                        查看更多历史版本
                    </el-button>
                    <el-button v-else disabled link>到底了</el-button>
                </template>
            </div>
        </div>

        <DownloadAll ref="downloadAll"/>
    </div>
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import * as api from "./api.js"
import VersionCell from "./Components/VersionCell.vue";
import DownloadAll from "./DownloadAll.vue"


const {$format} = window

const props = defineProps({});
const emits = defineEmits();
const filterVersionList = computed(() => state.versionList.filter(item => item.id !== state.currentVersion.id))


// 路由
const router = useRouter();
// store
const store = useStore();
const state = reactive({
    programList: [],
    currentProgram: {},
    currentVersion: {},
    versionList: [],
    showHistoryVersion: true
})

// 请求query参数
const requestParams = reactive({
    orderCol: "createdTime",
    isDesc: true,
    pageIndex: 1,
    pageSize: 20,
});
// 请求body参数
const requestBody = reactive({
    name: null,
})
// 分页配置
const pageConfig = reactive({
    total: 0,
    sizes: [10, 20, 50, 100, 200],
    totalPages: 0,
})


function toIndex() {
    window.location = import.meta.env.VITE_API_Index_URL
}

/**
 * 格式化 description
 * @param text
 * @returns {*|string}
 */
function description(text) {
    if (!text) return "";
    if (text.indexOf("<p>") > -1) return text;
    if (text.indexOf("\n") > -1) {
        const textList = text.split("\n");
        return textList.map(item =>  `<p>${item}</p>`).join("");
    }
}

/**
 * 选中程序
 * @param item
 */
function programSelect(item) {
    state.currentProgram = item || {}
    if (state.currentProgram.id) {
        loadCurrentVersion()
        state.versionList = [];
        state.showHistoryVersion = true;
        requestParams.pageIndex = 1;
        pageConfig.total = 0;
        pageConfig.totalPages = 0;
    }
}

/**
 * 下载全量包
 */
const downloadAll = ref()
async function downloadAllPackage(row) {
    downloadAll.value.show({
        title: "下载",
        options: {
            programId:  row.programId,
            versionNum: row.versionNum,
            versionBitness: row.versionBitness,
            fileName: `${state.currentProgram.name}_V${row.versionNum}` ,
        }
    })
}

/**
 * 查询版本列表
 * @returns {Promise<number>}
 */
async function loadVersionList() {
    const params = {
        ...requestParams
    };
    const body = {
        ...requestBody,
        programId: state.currentProgram.id,
    };
    state.isLoading = true;
    const {success, data} = await api.GetVersionPageList(params, body);
    state.isLoading = false;
    if (!success) return 0;
    const {items, totalCount, totalPages, pageIndex} = data;
    if (params.pageIndex === 1) {
        state.versionList = items;
    } else {
        state.versionList = state.versionList.concat(items);
    }

    pageConfig.total = totalCount;
    pageConfig.totalPages = totalPages;
    requestParams.pageIndex = pageIndex;
}

/**
 * 查询下一页
 */
function nextPage() {
    requestParams.pageIndex += 1;
    loadVersionList()
}

/**
 * 加载 历史版本
 */
function reLoadVersionList() {
    state.showHistoryVersion = false
    loadVersionList()
}

/**
 * 查询程序列表
 * @returns {Promise<void>}
 */
async function loadProgramList() {
    const {data} = await api.GetProgramList();
    state.programList = data || [];
    programSelect((data || [])[0])
}

/**
 * 查询最新版本
 * @returns {Promise<void>}
 */
async function loadCurrentVersion() {
    const {data} = await api.GetNewVersion({programId: state.currentProgram.id});
    state.currentVersion = data || {};
}


onMounted(() => {
    loadProgramList()
    // const wow = new $Wow({
    //     boxClass: 'animate__animated',
    //     animateClass: 'animate__animated',
    //     offset: 10,
    //     mobile: true,
    //     live: true,
    //     callback: (box) => {},
    //     scrollContainer: null // optional scroll container selector, otherwise use window
    // });
    // wow.init();
})
</script>

<style scoped lang="scss">
.Index {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;
    background: #fff;
    box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);
    height: calc(100vh - 110px);
    overflow: auto;

    .tabs {
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        height: 60px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        backdrop-filter: blur(10px);
        border-bottom: 1px solid #e0e0e0;
        background: rgba(255, 255, 255, 0.8);
        user-select: none;

        .program {
            font-size: 18px;
            cursor: pointer;
            color: #333;
            position: relative;
            height: 100%;
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            padding: 0 20px;

            .bottom-line {
                position: absolute;
                bottom: 0;
                left: 0;
                right: 0;
                height: 4px;
                background: var(--el-color-primary);
            }
        }
    }
    .container {
        width: 1000px;
        padding: 40px 10px 10px 10px;
        margin: 0 auto;
        color: #333;
        .current-version {
            border: 2px solid var(--el-color-primary);
            border-radius: 12px;
            .info {
                display: flex;
                flex-direction: row;
                justify-content: space-between;
                align-items: flex-start;
                border-radius: inherit;
                padding: 20px;
                box-sizing: border-box;
                background: #fff;
                user-select: none;

                .left {
                    display: flex;
                    flex-direction: column;
                    justify-content: flex-start;
                    align-items: flex-start;
                    flex: 1;
                    padding-right: 20px;


                    .program-name {
                        font-size: 30px;
                        font-weight: bold;
                        color: var(--el-color-primary);
                        display: flex;
                        flex-direction: row;
                        justify-content: flex-start;
                        align-items: center;
                        .logo {
                            width: 50px;
                            height: 50px;
                        }
                        .name {
                            padding-left: 20px;
                        }
                    }
                    .program-desc {
                        margin-top: 20px;
                        font-size: 16px;
                        line-height: 30px;
                        width: 100%;
                        word-break: break-all;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        -webkit-line-clamp: 2; /* 设置超出的行数 */
                        overflow: hidden;
                    }
                }
                .right {
                    margin-left: 10px;
                    width: 300px;
                    box-sizing: border-box;
                    padding-left: 20px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    align-items: center;
                    height: 100%;
                    border-left: 1px solid #e0e0e0;

                    .version-num {
                        font-size: 20px;
                        font-weight: bold;
                        color: var(--el-color-primary);
                    }
                    .time {
                        margin-top: 10px;
                        font-size: 16px;
                        color: #666;
                    }

                    .download-button {
                        margin-top: 10px;
                        height: 60px;
                        color: #fff;
                        background: var(--el-color-primary);
                        width: 100%;
                        display: flex;
                        flex-direction: row;
                        justify-content: center;
                        align-items: center;
                        cursor: pointer;
                        border-radius: 12px;
                        font-weight: bold;
                        font-size: 20px;
                        box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0);
                        transition: all 0.1s ease;

                        .version-number {
                            padding-right: 20px;
                        }

                        .text {
                            font-size: 20px;
                        }

                        &:hover {
                            background: #1f89fa;
                            box-shadow: 4px 0 20px 0 rgba(0, 0, 0, 0.3);
                        }

                        &:active {
                            box-shadow: 4px 0 20px 0 rgba(0, 0, 0, 0.5);
                        }
                    }

                }
            }
            .log-list {
                width: 100%;
                box-sizing: border-box;
                background: var(--el-color-primary-light-9);
                border-radius: 0 0 12px 12px;
                //padding: 20px;
                display: flex;
                flex-direction: row;
                justify-content: center;
                align-items: center;

                .title {
                    font-size: 18px;
                    font-weight: bold;
                    color: var(--el-color-primary);
                    width: 160px;
                    text-align: center;
                }

                .log-content {
                    width: calc(100% - 160px);
                    height: 170px;
                    overflow: auto;
                    padding: 10px;
                    box-sizing: border-box;

                    img {
                        max-width: 60%;
                    }
                }
                .placeholder {
                    display: flex;
                    flex-direction: row;
                    justify-content: flex-start;
                    align-items: center;
                }
            }
        }


        .history-version {
            padding-top: 20px;
            width: 100%;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;

            .list {
                width: 1000px;
                .title {
                    font-size: 20px;
                    font-weight: bold;
                    color: var(--el-color-primary);
                    padding: 10px 0;
                }
            }
        }
    }

}
</style>